namespace C1.Win.C1FlexGrid
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Reflection;

    public class ColumnCollection : RowColCollection
    {
        internal string _colDef;

        internal ColumnCollection(GridModel grid, int nItems, int nFixed, int defaultSize)
        {
            base._grid = grid;
            base._fixed = nFixed;
            base._defaultSize = defaultSize;
            base._lastVisible = -1;
            base._items = new ArrayList(nItems);
            for (int i = 0; i < nItems; i++)
            {
                base._items.Add(new Column(this, i));
            }
            base.SetDirty();
        }

        public ColumnCollection(int nItems, int nFixed, int szMin, int szMax, int szDefault, string colDefs)
        {
            base._grid = null;
            base._fixed = nFixed;
            base._frozen = 0;
            base._minSize = szMin;
            base._maxSize = szMax;
            base._defaultSize = szDefault;
            base._lastVisible = -1;
            base._items = new ArrayList(nItems);
            for (int i = 0; i < nItems; i++)
            {
                base._items.Add(new Column(this, i));
            }
            this._colDef = colDefs;
            base.SetDirty();
        }

        public ColumnCollection(int nItems, int nFixed, int nFrozen, int szMin, int szMax, int szDefault, string colDefs)
        {
            base._grid = null;
            base._fixed = nFixed;
            base._frozen = nFrozen;
            base._minSize = szMin;
            base._maxSize = szMax;
            base._defaultSize = szDefault;
            base._lastVisible = -1;
            base._items = new ArrayList(nItems);
            for (int i = 0; i < nItems; i++)
            {
                base._items.Add(new Column(this, i));
            }
            this._colDef = colDefs;
            base.SetDirty();
        }

        public Column Add()
        {
            return this.Insert(this.Count);
        }

        internal string BuildString(bool includeDefault)
        {
            string str = string.Empty;
            for (int i = 0; i < this.Count; i++)
            {
                string str2 = this[i].BuildString(includeDefault);
                if ((str2 != null) && (str2.Length != 0))
                {
                    string str3 = str;
                    str = str3 + i.ToString() + "{" + str2 + "}\t";
                }
            }
            return str;
        }

        public ColumnCollection GetSelection()
        {
            ArrayList list = new ArrayList();
            foreach (Column column in base._items)
            {
                if (column.Selected)
                {
                    list.Add(column);
                }
            }
            ColumnCollection columns = new ColumnCollection(base._grid, 0, 0, base._defaultSize);
            columns._items = list;
            return columns;
        }

        public Column Insert(int index)
        {
            base._items.Insert(index, new Column(this, this.Count));
            base.SetDirty();
            base.NotifyViews(GridChangedTypeEnum.ColAdded, -1, index);
            return this[index];
        }

        public void InsertRange(int index, int count)
        {
            for (int i = 0; i < count; i++)
            {
                base._items.Insert(index, new Column(this, this.Count));
            }
            base.SetDirty();
            base.NotifyViews(GridChangedTypeEnum.ColAdded, index, -1);
        }

        public override void Move(int indexOld, int indexNew)
        {
            base.Move(indexOld, indexNew);
            base.NotifyViews(GridChangedTypeEnum.ColMoved, -1, indexOld, -1, indexNew);
        }

        public override void MoveRange(int index, int count, int indexNew)
        {
            base.MoveRange(index, count, indexNew);
            base.NotifyViews(GridChangedTypeEnum.ColMoved, -1, index, -1, indexNew);
        }

        internal bool ParseString(string str)
        {
            while (true)
            {
                int index = str.IndexOf('{');
                int num2 = str.IndexOf('}');
                if ((index < 0) || (num2 < 0))
                {
                    break;
                }
                int num3 = -1;
                try
                {
                    num3 = int.Parse(str.Substring(0, index).Trim());
                }
                catch
                {
                }
                if ((num3 >= 0) && (num3 < this.Count))
                {
                    string str2 = str.Substring(index + 1, (num2 - index) - 1).Trim();
                    this[num3].ParseString(str2);
                }
                str = str.Substring(num2 + 1);
            }
            base.SetDirty();
            return true;
        }

        public Column Remove(int index)
        {
            Column column = this[index];
            this.RemoveAt(index);
            base.SetDirty();
            base.NotifyViews(GridChangedTypeEnum.ColRemoved, -1, index);
            return (column.Detach() as Column);
        }

        internal void RemoveAt(int index)
        {
            int num = this[index]._dataIndex;
            if (base._grid != null)
            {
                foreach (Row row in base._grid._rows)
                {
                    ArrayList list = row._cellData;
                    if ((list != null) && (list.Count > num))
                    {
                        list.RemoveAt(num);
                    }
                    list = row._cellInfo;
                    if ((list != null) && (list.Count > num))
                    {
                        list.RemoveAt(num);
                    }
                }
            }
            foreach (Column column in base._items)
            {
                if (column._dataIndex > num)
                {
                    column._dataIndex--;
                }
            }
            base._items.RemoveAt(index);
        }

        public void RemoveRange(int index, int count)
        {
            for (int i = 0; i < count; i++)
            {
                ((RowCol) base._items[index]).Detach();
                base._items.RemoveAt(index);
            }
            base.SetDirty();
            base.NotifyViews(GridChangedTypeEnum.ColRemoved, index, -1);
        }

        [DefaultValue(10)]
        public override int Count
        {
            get
            {
                return base._items.Count;
            }
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Number of Columns must be positive");
                }
                int count = base._items.Count;
                if (value > count)
                {
                    for (int i = count; i < value; i++)
                    {
                        base._items.Add(new Column(this, i));
                    }
                    base.SetDirty();
                    base.NotifyViews(GridChangedTypeEnum.ColAdded);
                }
                if (value < count)
                {
                    while (value < count)
                    {
                        this.RemoveAt(count - 1);
                        count--;
                    }
                    base.SetDirty();
                    base.NotifyViews(GridChangedTypeEnum.ColRemoved);
                }
            }
        }

        public override int Fixed
        {
            get
            {
                return base._fixed;
            }
            set
            {
                if (base._grid.IsDataBound())
                {
                    while (value > base._fixed)
                    {
                        base._fixed++;
                        this.Insert(base._fixed - 1);
                    }
                    while (value < base._fixed)
                    {
                        base._fixed--;
                        this.Remove(base._fixed);
                    }
                }
                else
                {
                    base.Fixed = value;
                }
            }
        }

        public Column this[int index]
        {
            get
            {
                return (Column) base._items[index];
            }
        }

        public Column this[string name]
        {
            get
            {
                foreach (Column column in base._items)
                {
                    if (string.Compare(name, column.Name, true) == 0)
                    {
                        return column;
                    }
                }
                return null;
            }
        }
    }
}

